﻿using Caliburn.Micro;
using MusicExplorer.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media.Imaging;
using MusicExplorer.Extensions;
using Windows.UI.Xaml.Media;
using Windows.UI;
using MusicExplorer.Repository;

namespace MusicExplorer.ViewModels.Wrappers
{
    public sealed class TrackTileViewModel : PropertyChangedBase
    {
        private readonly Track m_track;
        private readonly PageViewModelBase m_parent;
        private readonly int m_index;

        private TrackTileViewModel(PageViewModelBase parent, Track track, int index)
        {
            m_track = track;
            m_parent = parent;
            m_index = index;
        }

        public static async Task<TrackTileViewModel> CreateAsync(PageViewModelBase parent, Track track, int index)
        {
            var wrapper = new TrackTileViewModel(parent, track, index);
            await wrapper.InitializeAsync();
            return wrapper;
        }

        public async Task InitializeAsync()
        {
            m_repositoryIcon = new BitmapImage();
            await m_repositoryIcon.FillWithIconRepositoryAsync(m_track.Repository);
            NotifyOfPropertyChange(() => RepositoryIcon);
        }

        private BitmapImage m_repositoryIcon;
        public BitmapImage RepositoryIcon { get { return m_repositoryIcon; } }

        public Track Track { get { return m_track; } }
        public PageViewModelBase Parent { get {return m_parent; } }
        public string Artwork { get { return m_track.Artwork; } }
        public string DisplayName { get { return m_track.Name; } }
        public DateTime PublishDate { get { return m_track.PublishDate; } }
        public string DisplayDate { get { return m_track.PublishDate.ToString("d", new CultureInfo("en-US")); } }
        public string DisplayComposers
        {
            get
            {
                if (m_track.Artists.ContainsKey(ArtistRole.Composer))
                {
                    return string.Join(", ", m_track.Artists[ArtistRole.Composer].Select(a => a.Name));
                }
                else return null;
            }
        }
        public string DisplayRemixers
        {
            get
            {
                if (m_track.Artists.ContainsKey(ArtistRole.Remixer))
                {
                    return string.Join(", ", m_track.Artists[ArtistRole.Remixer].Select(a => a.Name));
                }
                else return null;
            }
        }

        public string WebButtonLabel { get { return m_track.Repository.Name; } }

        public int Index { get { return m_index; } }
        public TrackType TrackType { get { return m_track.Type; } }

        public bool IsRead
        {
            get { return m_track.IsRead; }
        }

        private bool m_isSelected;
        public bool IsSelected
        {
            get { return m_isSelected; }
            set
            {
                if(m_isSelected != value)
                {
                    m_isSelected = value;
                    NotifyOfPropertyChange(() => IsSelected);
                    NotifyOfPropertyChange(() => PrimaryForeground);
                    NotifyOfPropertyChange(() => PrimaryBackground);
                    NotifyOfPropertyChange(() => SecondaryForeground);
                }
            }
        }

        /// UI
        /// ..
        public SolidColorBrush PrimaryBackground
        {
            get
            {
                var brush = new SolidColorBrush();
                if (m_isSelected)
                    brush.Color = Color.FromArgb(255, 0x00, 0x77, 0x00);
                else
                {
                    if (m_index % 2 == 0)
                        brush.Color = Colors.Transparent;
                    else
                        brush.Color = Color.FromArgb(255, 0xE2, 0xE2, 0xE2);
                }
                return brush;
            }
        }

        public SolidColorBrush PrimaryForeground
        {
            get
            {
                var brush = new SolidColorBrush();
                if (m_isSelected)
                    brush.Color = Color.FromArgb(255, 0xFF, 0xFF, 0xFF);
                else
                    brush.Color = Color.FromArgb(255, 0x22, 0x22, 0x22);
                return brush;
            }
        }

        public SolidColorBrush SecondaryForeground
        {
            get
            {
                var brush = new SolidColorBrush();
                if (m_isSelected)
                    brush.Color = Color.FromArgb(255, 0xFF, 0xFF, 0xFF);
                else
                    brush.Color = Color.FromArgb(255, 0x80, 0x80, 0x80);
                return brush;
            }
        }
    }
}
